from django import forms
from .models import MemeQuestions
import random

class LoginForm(forms.Form):
	name = forms.CharField(max_length=20, label="账号", widget=forms.TextInput(attrs={'class':'form-control'}))
	password = forms.CharField(max_length=100, label="密码", widget=forms.PasswordInput(attrs={'class':'form-control'}))

class RegisterForm(forms.Form):
	name = forms.CharField(max_length=20, label="账号", widget=forms.TextInput(attrs={'class':'form-control'}))
	true_name = forms.CharField(max_length=10, label='你的真名', widget=forms.TextInput(attrs={'class':'form-control'}))
	password = forms.CharField(max_length=100, label="密码", widget=forms.PasswordInput(attrs={'class':'form-control'}))
	repeat_password = forms.CharField(max_length=100, label="重复密码", widget=forms.PasswordInput(attrs={'class':'form-control'}))
	code = forms.CharField(max_length=20, label="邀请码", widget=forms.TextInput(attrs={'class':'form-control'}))
	meme_answer = forms.CharField(label='', widget=forms.TextInput(attrs={'class':'form-control'}))
	def __init__(self, *args, **kwargs):
		super().__init__(*args, **kwargs)
        # 在表单实例化时查询随机问题
		self.meme = MemeQuestions.objects.all().order_by('?').first()
		if self.meme:
			self.fields['meme_answer'].label = self.meme.question
            # 存储问题ID用于后续验证
			self.meme_id = self.meme.id
		else:
            # 处理没有问题的情况
			self.fields['meme_answer'].label = "无可用问题（也就是说，无法注册）"
			self.meme_id = None

class RenameForm(forms.Form):
	now_name = forms.CharField(max_length=20, label="原账号名", widget=forms.TextInput(attrs={'class':'form-control'}))
	password = forms.CharField(max_length=100, label="密码", widget=forms.PasswordInput(attrs={'class':'form-control'}))
	new_name = forms.CharField(max_length=20, label="新账号名", widget=forms.TextInput(attrs={'class':'form-control'}))
	code = forms.CharField(max_length=20, label="改名卡ID", widget=forms.TextInput(attrs={'class':'form-control'}))
